package com.sci99.ex.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.sci99.ex.model.Employee;
import com.sci99.ex.util.MyBatisUtil;

public class EmpService implements UserDetailsService {
	static Logger log = Logger.getLogger(EmpService.class);

	/**
	 * 查询员工信息，返回前100条记录
	 * 
	 * @param parameter
	 * @return
	 * @throws Exception
	 */
	public static List<Employee> getEmployee(Map<String, Object> parameter)
			throws Exception {
		SqlSession s = MyBatisUtil.getMsSqlSession();
		List<Employee> rs = s.selectList("getEmployee", parameter);
		s.close();
		return rs;
	}

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		Employee emp;
		try {
			Long eid = Long.parseLong(username);
			SqlSession s = MyBatisUtil.getMsSqlSession();
			Map<String, Object> args = new HashMap<>();
			args.put("eid", eid);
			emp = s.selectOne("getEmployee", args);
			s.close();
		} catch (IOException e) {
			log.error(e.getMessage());
			throw new UsernameNotFoundException(username);
		}
		return emp;
	}

}
